#include <benchmark/benchmark.h>
#include "sys/stat.h"
#include "fcntl.h"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "sys/types.h"
#include "util.h"

using namespace std;

static void Bm_function_Fstatat(benchmark::State &state)
{
    int fd1, fd2;
    struct stat st;
    fd1 = open("/data/test.txt", O_RDONLY);
    if (fd1 == -1)
    {
        perror("open fstatat");
        exit(EXIT_FAILURE);
    }

    fd2 = fstatat(fd1, "/data/test.txt", &st, 0);
    if (fd2 == -1)
    {
        perror("fstatat");
        exit(EXIT_FAILURE);
    }
    for (auto _ : state)
    {
        benchmark::DoNotOptimize(fd2);
    }
    close(fd1);
    state.SetBytesProcessed(state.iterations());
}

static void Bm_function_Fstat64(benchmark::State &state)
{
    struct stat buf;
    int fd = open("/etc/passwd", O_RDONLY);
    if (fd == -1)
    {
        perror("open fstat64");
        exit(EXIT_FAILURE);
    }

    for (auto _ : state)
    {
        benchmark::DoNotOptimize(fstat(fd, &buf));
    }
    close(fd);
    state.SetItemsProcessed(state.iterations());
}

static void Bm_function_Mkdir(benchmark::State &state)
{
    for (auto _ : state)
    {
        benchmark::DoNotOptimize(mkdir("/data/data/test_mkdir", S_IRWXU | S_IRWXG | S_IXGRP | S_IROTH | S_IXOTH));
    }
    state.SetItemsProcessed(state.iterations());
}

MUSL_BENCHMARK(Bm_function_Fstatat);
MUSL_BENCHMARK(Bm_function_Fstat64);
MUSL_BENCHMARK(Bm_function_Mkdir);